package gemini.castor.ui.client.page.content.store.shoppingcart.shoppingcartview;

import gemini.castor.ui.client.constants.I18nConstants;
import gemini.castor.ui.client.page.content.store.shoppingcart.cartsummary.ShoppingCartSummaryWidget;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiTemplate;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

public class ShoppingCartViewView extends Composite implements
		ShoppingCartViewPresenter.Display {

	@UiTemplate("ShoppingCartViewView.ui.xml")
	interface MyUiBinder extends UiBinder<Panel, ShoppingCartViewView> {
	}

	private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

	private ShoppingCartViewObject object;

	@Override
	public void setObject(ShoppingCartViewObject object) {
		this.object = object;
	}
	private I18nConstants constants;

	public ShoppingCartViewView() {
		initWidget(binder.createAndBindUi(this));
		constants = GWT.create(I18nConstants.class);
	}
	
	@UiField
	ShoppingCartSummaryWidget owgOrderSummary;
	
	@Override
	public ShoppingCartSummaryWidget getOwgOrderSummary() {
		return owgOrderSummary;
	}

	@UiField
	HTML htmEarnBase;
	
	@UiField
	HTML htmRetailPrice;
	
	@UiField
	HTML htmDiscountedRetail;
	
	@UiField
	HTML htmPoint;
	
	@UiField
	HTML htmOrderMonth;
	
	@UiField
	HTML htmMonthlyPoint;
	
	@UiField
	HTML htmDiscountScale;
	
	@UiField
	Button btnCheckOut;
	
	@UiField
	Button btnAddMoreProducts;
	
	@UiField
	Button btnCancelOrder;
	
	public void recalculate() {
		object.recalculate();
		
		htmEarnBase.setHTML(String.valueOf(object.getEarnBase()));
		htmRetailPrice.setHTML(String.valueOf(object.getRetailPrice()));
		htmDiscountedRetail.setHTML(String.valueOf(object.getDiscountedRetail()));
		htmPoint.setHTML(String.valueOf(object.getPoint()));
		htmOrderMonth.setHTML(object.getOrderMonth());
		htmMonthlyPoint.setHTML(String.valueOf(object.getMonthlyPoint()));
		htmDiscountScale.setHTML(String.valueOf(object.getDiscountScale()));
	}
	
	@Override
	public void loadData(){
		recalculate();
		owgOrderSummary.loadData(object.getItems(), true);
		
		if (object.getItems() != null && !object.getItems().isEmpty()) {
			btnCancelOrder.setEnabled(true);
			btnCheckOut.setEnabled(true);
		} else {
			btnCancelOrder.setEnabled(false);
			btnCheckOut.setEnabled(false);
		}
	}

	@Override
	public Widget asWidget() {
		return this;
	}

	@Override
	public HasClickHandlers getCancelCartClickHandlers() {
		return btnCancelOrder;
	}

	@Override
	public String getConfirmMessage() {
		return constants.htmAreYouSure();
	}

	@Override
	public ShoppingCartViewObject getObject() {
		return object;
	}

	@Override
	public HasClickHandlers getAddMoreCartClickHandlers() {
		return btnAddMoreProducts;
	}

	@Override
	public HasClickHandlers getCheckOutClickHandlers() {
		return btnCheckOut;
	}
}
